package com.hehe.httpurlconnectiontest2;

import android.util.Log;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by lenove on 2017/11/7.
 * 获取数据类
 * 所有的数据获取方法都在这里
 *
 */

public class GetDataFromYeelink {
    //TODO 之后还要再加上获取ApiKey的方法
    //GET方法
    // path为我们要访问的URL
    public static String GET(String path,String ApiKey) throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时事件
        conn.setConnectTimeout(5000);
        //请求方法为GET
        conn.setRequestMethod("GET");
        //允许输入
        conn.setDoInput(true);
        //将U-ApiKey加入请求头
        conn.setRequestProperty("U-ApiKey",ApiKey);
        //关闭缓存
        conn.setUseCaches(false);
        //获取http响应码,响应码为200表示服务器响应成功，不需要显式调用HttpURLConnection.connect()方法，因为getResponseCode()方法中已经调用，http请求也一并发出
        int response_code = conn.getResponseCode();

        Log.i("GetData.response",":"+response_code);

        if (response_code==200){
            //获取输入流
            InputStream in = conn.getInputStream();
            //拿到数据
            byte[] data = StreamTool.read(in);
            String data_str = new String(data,"UTF-8");
            //Log.i("GetData.html",":"+data_str);
            return data_str;
        }else {
            //如果连接异常，返回响应码
            return String.valueOf(response_code);
        }
    }

    //不带ApiKey的GET方法
    // path为我们要访问的URL
    public static String GET(String path) throws Exception{
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时事件
        conn.setConnectTimeout(5000);
        //请求方法为GET
        conn.setRequestMethod("GET");
        //允许输入
        conn.setDoInput(true);
        //关闭缓存
        //conn.setUseCaches(false);
        //获取http响应码,响应码为200表示服务器响应成功，不需要显式调用HttpURLConnection.connect()方法，因为getResponseCode()方法中已经调用，http请求也一并发出
        int response_code = conn.getResponseCode();

        Log.i("GetData.response",":"+response_code);

        if (response_code==200){
            //获取输入流
            InputStream in = conn.getInputStream();
            //拿到数据
            byte[] data = StreamTool.read(in);
            String data_str = new String(data,"UTF-8");
            //Log.i("GetData.html",":"+data_str);
            return data_str;
        }else {
            //如果连接异常，返回响应码
            return String.valueOf(response_code);
        }
    }

    //通过用户名和密码获取ApiKey
    public static String getApiKey(User user)
    {
        String data = null;
        try {
            data = GET("http://api.yeelink.net/v1.0/user/apikey?username="+user.getUsername()+"&pass="+user.getPass());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /*
     * 罗列设备
     *[
     *  {
     *    "id": "2",
     *    "title": "test2",
     *    "about": "just a test"
     *   },
     *  {
     *    "id": "3",
     *    "title": "test3",
     *    "about": "just a test"
     *   }
     * ]
     */
    public static String listDevices(String ApiKey){
        String data_str = null;
        try {
            data_str = GET("http://api.yeelink.net/v1.0/devices",ApiKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data_str;
    }

    //罗列传感器
    //返回的数据格式
    //[
    //  {
    //    "id": 2,
    //    "title": "test2",
    //    "about": "just a test",
    //    "type":0,
    //    "last_update": 1380009649,
    //    "last_data":"317",
    //    "last_data_gen":null
    //	},
    //	{
    //    "id": 3,
    //    "title": "test3",
    //    "about": "just a test",
    //    "type":6,
    //    "last_update": 1380009669,
    //    "last_data":null;,
    //    "last_data_gen":{"lat":23.8,"lng":54.5,"speed":45}
    //  }
    //]
    public static String listSensors(String ApiKey,String device_id){
        String data_str = null;
        try {
            data_str = GET("http://api.yeelink.net/v1.0/device/"+device_id+"/sensors",ApiKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data_str;
    }

    //查看传感器的详细信息
    public static String checkSensor(String ApiKey,String device_id ,String sensor_id){
        String data_str = null;
        try {
            data_str = GET("http://api.yeelink.net/v1.0/device/"+device_id+"/sensor/"+sensor_id,ApiKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data_str;
    }

    /*
     *查看数据点
     * 返回的数据格式为JSON
     *  {
     *      "timestamp":"2012-03-15T16:13:14",
     *      "value": 39.2
     *   }
     *
     */
    public static String listDatapoint(String ApiKey,String device_id,String sensor_id){
        String data_str = null;
        try {
            data_str = GET("http://api.yeelink.net/v1.0/device/"+device_id+"/sensor/"+sensor_id+"/datapoint/",ApiKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data_str;
    }
}
